<?php
namespace App\Http\Controllers\Client\Activity\Talent;

use App\Http\Controllers\ClientController;
use App\Models\Activity;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ActivityDetailController extends ClientController
{

    public function index()
    {

        $all = $this->all;
        $act_id = $all['act_id'];
        $errMsg = '';

        try{
            // 查询活动信息
            $result = DB::table('lottery_activity as la')
                ->leftJoin('lottery_goods as lg', 'la.goods_id', '=', 'lg.goods_id')
                ->leftJoin('shop', 'la.shop_id', '=', 'shop.shop_id')
                ->leftJoin('dict_area as da', 'shop.district', '=', 'da.id')
                ->where('la.act_id', '=', $act_id)
                ->select('la.act_id','la.act_no','la.goods_id','la.shop_id','la.start_time','la.end_time','la.draw_time','la.status','la.is_stop',
                    'lg.goods_name','lg.title_img','lg.goods_img','lg.price','lg.detail',
                    'shop.shop_name', 'shop.shop_type', 'shop.contact_man', 'shop.province', 'shop.city', 'shop.district', 'shop.address', 'shop.link_url',
                    'da.short_name')
                ->first();
            $result = json_decode(json_encode($result), true);
            $result['goods_img'] = explode(',', $result['goods_img']);
            $result['goods_imgs'] = AliOss::getUrls($result['goods_img']);
            $result['title_img'] = AliOss::getUrl($result['title_img']);
            $result['shop_type_name'] = Data::$laifeiShopTypes[$result['shop_type']];
            $result['total'] = DB::table('lottery_users_draw')->where('act_id', '=', $act_id)->count();

            // 查询我的参与信息
            $result['participate_status'] = DB::table('lottery_users_draw')
                ->where('act_id', '=', $act_id)
                ->where('we_id', '=', $this->we_id)
                ->value('status');

            // 查询是否已经赢得过这个奖品
            $wined = in_array($result['shop_id'],Activity::winedShopIds($this->we_id));
            // 是否正在参与这个奖品的其他开奖
            $pending = in_array($result['shop_id'],Activity::participatingShopIds($this->we_id));
            // 文案
            $result['text'] = Activity::activityStatus($result['status'],$result['participate_status'],$wined,$pending);


            // 检查当前用户是否在商户所在城市
            $city = DB::table('talent')->where('we_id','=',$this->we_id)->value('city');
            if(!$city){
                $city = substr($all['adcode'],0,4).'00';
            }
            if((string)$city !== (string)$result['city']){
                $errMsg = '很抱歉，此活动不在您所在的城市';
            }

            return $this->setReturnJson(0,$errMsg,$result);
        } catch (\Exception $e) {
            Log::error(ActivityDetailController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '查询失败');
        }
    }

}
